home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / WebFace / Source / 20-NetworkServices / routing / static.frm.z / static.frm
Encoding:
Text File  |  1997-07-30  |  8.9 KB  |  342 lines

  1. #!/usr/bin/perl5
  2. #
  3. # static.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: static.frm,v 1.9 1997/06/19 22:26:31 shotes Exp $
  21.  
  22. require "/usr/OnRamp/lib/OnRamp.pm";
  23. require "/usr/OnRamp/lib/java.pm";
  24.  
  25. $conf = "/etc/config/static-route.options";
  26. $dummy = "/tmp/static.dummy";
  27. $myname = "static.cgi";
  28. $title = "Static Routing";
  29.  
  30. $js_main =
  31. "type = \"route\";
  32. $js_account_main
  33. $js_error_box
  34. $js_ip
  35. function testAdd(form) {
  36.     if (!testRoute(form.destination)) return (false);
  37.     return (true);
  38. }
  39. function testRoute(Ctrl) {
  40.     if (Ctrl.value == \"\") { 
  41.         errorBox (Ctrl, \"To add a new route, first enter \\na \"
  42.                 + \"destination IP address.\"); 
  43.         return (false); 
  44.     }
  45.     if (Ctrl.value == \"default\") return (true);
  46.     if (!testIPaddress(Ctrl.value,false)) { 
  47.         errorBox (Ctrl, \"The destination address \" + Ctrl.value 
  48.                 + \"\\nis invalid.\"); 
  49.         return (false); 
  50.     }
  51.     return (true);
  52. }";
  53.  
  54. $js_add =
  55. "$js_standard
  56. $js_error_box
  57. $js_ip
  58. function checkForm(form)  {
  59.     if (!testIP(form.route)) return (false);
  60.     return (true);
  61. }
  62. function testIP(Ctrl) {
  63.     if (Ctrl.value == \"\") { 
  64.         errorBox (Ctrl, \"The IP address of the router \\nis required.\"); 
  65.         return (false); 
  66.     }
  67.     if (!testIPaddress(Ctrl.value,false)) { 
  68.         errorBox (Ctrl, \"The router address \" + Ctrl.value 
  69.                 + \"\\nis invalid.\"); 
  70.         return (false); 
  71.     }
  72.     return (true);
  73. }";
  74.  
  75. $js_edit =
  76. "$js_standard
  77. $js_error_box
  78. $js_ip
  79. function checkForm(form) {
  80.     if (!testIP(form.route)) return (false);
  81.     return (true);
  82. }
  83. function testIP(Ctrl) {
  84.     if (Ctrl.value == \"\") { 
  85.         errorBox (Ctrl, \"The IP address of the router \\nis required.\"); 
  86.         return (false); 
  87.     }
  88.     if (!testIPaddress(Ctrl.value,false)) { 
  89.         errorBox (Ctrl, \"The router address \" + Ctrl.value 
  90.                 + \"\\nis invalid.\"); 
  91.         return (false); 
  92.     }
  93.     return (true);
  94. }";
  95.  
  96. print "Content-type: text/html\n\n";
  97.  
  98. &get_fields;
  99. &getGateways;
  100.  
  101. $delete = 0;
  102.  
  103. if (%fld) {
  104.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  105.     $help =~ s/cgi$/hlp/;
  106.     exec $help if ($fld{'help'} eq "Help");
  107.  
  108.     $fld{'chosen'} =~ /([\w.]+)/;
  109.     $fld{'chosen'} = $1;
  110.     if ($fld{"add"}) { 
  111.         &validAdd;
  112.         $val{'metric'} = 1;
  113.         &getAdd;
  114.         exit 0; 
  115.     }
  116.     elsif ($fld{"doAdd"}) {
  117.         &valid(0);
  118.         &doAdd;
  119.         $fld{'destination'} = "";
  120.         &getGateways;
  121.     }
  122.     elsif ($fld{"doEdit"}) {
  123.         &valid(1);
  124.         &doEdit;
  125.         &getGateways;
  126.     }
  127.     elsif ($fld{"edit"}) {
  128.         &error(2,"To edit an existing route, first choose one from the list,
  129.             then click the edit button.") if !$fld{'chosen'};
  130.         &getGateways($fld{'chosen'});
  131.     $val{'old_route'} = $rout;
  132.     $val{'old_type'} = $type;
  133.         &putEdit;
  134.         &getEdit;
  135.         exit 0; 
  136.     }
  137.     elsif ($fld{"delete"}) { 
  138.         &error(2,"To delete an existing route, first choose one from the 
  139.             list, then click the delete button.") if !$fld{'chosen'};
  140.         $delete = 1;
  141.         $message = qq|Click "Ok" to save changes.|; 
  142.     }
  143.     elsif ($fld{"doit"}) { 
  144.         if ($fld{'delGateway'}) { &doDelete; };
  145.         &getGateways;
  146.     }
  147.     else {
  148.         $message = "Use buttons to submit form.";
  149.     }
  150.  
  151.     if (!$message) { $message = "No change made."; }
  152.     %val = %fld; 
  153.  
  154. &generic;
  155.  
  156. sub valid {
  157.     &error($_[0],"Invalid route address.") if !$fld{'route'} || &check_ipaddr($fld{'route'});
  158.  
  159. sub validAdd {
  160.     &error(2,"You must provide a route address.") if !$fld{'destination'};
  161.     if ($fld{'destination'} ne "default") {
  162.         &error(2,"Invalid route address.") 
  163.         if &check_ipaddr($fld{'destination'});   
  164.     }
  165. }
  166.  
  167. sub error {
  168.     &error_block($_[1]);
  169.     %val = %fld;
  170.     if ($_[0] == 0) { &getAdd; }
  171.     elsif ($_[0] == 1) { &getEdit; }
  172.     else { &generic; }
  173.     exit 0;
  174. }
  175.  
  176. sub doDelete {
  177.     open(IN,"< $conf");
  178.     open(OUT,"> $dummy");
  179.     while(<IN>) {
  180.         @items = split(/\s+/);
  181.         if ($items[3] eq $fld{'delGateway'}) { 
  182.         $cmd = $_;
  183.         chop $cmd;
  184.         $cmd =~ s/add/delete/;
  185.         $cmd = "/usr/etc" . $cmd;
  186.         system("$cmd > /dev/null 2>&1");
  187.         print OUT "# $_"; 
  188.     }
  189.         else { print OUT $_; }
  190.     }
  191.     close(IN);
  192.     close(OUT);
  193.     rename($dummy,$conf);
  194.  
  195.     $message .= "Route deleted.";
  196. }
  197.  
  198. sub doAdd {
  199.     $cmd = "/usr/etc/route add -$fld{'type'} $fld{'destination'} $fld{'route'}";
  200.     system("$cmd > /dev/null 2>&1");
  201.  
  202.     open(OUT,">> $conf");
  203.     print OUT "$cmd\n";
  204.     close(OUT);
  205.  
  206.     $message = "Route added.";
  207. }
  208.  
  209. sub putEdit {
  210.     $val{'type'} = $type;
  211.     $val{'route'} = $rout;
  212. }
  213.  
  214. sub doEdit {
  215.     $cmd2 = "/usr/etc/route delete -$fld{'old_type'} $fld{'chosen'} $fld{'old_route'}";
  216.     system("$cmd2 > /dev/null 2>&1");
  217.  
  218.     $cmd1 = "/usr/etc/route add -$fld{'type'} $fld{'chosen'} $fld{'route'}";
  219.     system("$cmd1 > /dev/null 2>&1");
  220.  
  221.     open(IN,"< $conf");
  222.     open(OUT,"> $dummy");
  223.     while(<IN>) {
  224.     @items = split(/\s+/);
  225.     if ($items[3] eq $fld{'chosen'}) { print OUT "# $_"; }
  226.     else { print OUT $_; }
  227.     }
  228.     print OUT "$cmd1\n";
  229.     close(IN);
  230.     close(OUT);
  231.     rename($dummy,$conf);
  232.  
  233.     $message .= "Route edited.";
  234. }
  235.     
  236. sub getEdit {
  237.     &js_title_block($title,$js_edit);
  238.     &header_block("Edit Route");
  239.  
  240.     print "<form name=StandardForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  241.  
  242.     print "<input type=hidden name=chosen value=$fld{'chosen'}>\n";
  243.     print "<input type=hidden name=old_route value=$val{'old_route'}>\n";
  244.     print "<input type=hidden name=old_type value=$val{'old_type'}>\n";
  245.  
  246.     print "<center><table width=450>";
  247.  
  248.     print "<tr><th align=left>IP address of route destination:</th><td>
  249.         <tt>$fld{'chosen'}</tt></td></tr>";
  250.     print "<tr><th align=left>IP address of router:</th><td>",
  251.         &text('route',$val{'route'},16),"</td></tr>";
  252.     print "<tr><th align=left>Type of network route:</th><td>",
  253.         &select('type',$val{'type'},'net','host'),"</td></tr>";
  254.  
  255.     print "</table></center>";
  256.  
  257.     print &js_buttons('doEdit','Ok','onClick="markOK()"','onClick="markOther()"');
  258.     
  259.     print '</form>';
  260. }
  261.  
  262. sub getAdd {
  263.     &js_title_block($title,$js_add);
  264.     &header_block("Add New Route");
  265.  
  266.     print "<form name=StandardForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  267.  
  268.     print "<input type=hidden name=destination value=$fld{'destination'}>";
  269.  
  270.     print "<center><table width=450>";
  271.  
  272.     print "<tr><th align=left>IP address of route destination:</th><td>
  273.         <tt>$fld{'destination'}</tt></td></tr>";
  274.     print "<tr><th align=left>IP address of router:</th><td>",
  275.         &text('route',$val{'route'},16),"</td></tr>";
  276.     print "<tr><th align=left>Type of network route:</th><td>",
  277.         &select('type',$val{'type'},'net','host'),"</td></tr>";
  278.  
  279.     print "</table></center>";
  280.  
  281.     print &js_buttons('doAdd','Ok','onClick="markOK()"','onClick="markOther()"');
  282.     
  283.     print '</form>';
  284. }
  285.    
  286. sub generic {
  287.     &js_title_block($title,$js_main);
  288.     &header_block($title);
  289.  
  290.     print "<i>$message</i>";
  291.  
  292.     print "<form name=AccountForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  293.  
  294.     print "<center><table width=450>\n";
  295.  
  296.     print "<tr><th align=left>";
  297.     print qq|<input type=submit name=add value="Add New Route" onClick="markAdd()">|;
  298.     print "\n</th><td><input size=19 type=text name=destination 
  299.         value=$val{'destination'}>";
  300.     print "</td></tr>\n";
  301.  
  302.     if ($delete) { 
  303.         undef @locList;
  304.         foreach $arg (@routes) {
  305.             if ($arg ne $fld{'chosen'}) { 
  306.             push(@locList,$arg); }
  307.         }
  308.         print "<input type=hidden name=delGateway value=$fld{'chosen'}>"; 
  309.     } else { @locList = @routes; }
  310.  
  311.     print "<tr><th align=left>";
  312.     print qq|<input type=submit name=edit value="Edit Selected Route" onClick="markEdit()">|;
  313.     print "\n</th><td rowspan=2>", &choice_list(*locList,"chosen",20), "</td></tr>\n";
  314.  
  315.     print "<tr><th align=left>";
  316.     print qq|<input type=submit name=delete value="Delete Selected Route" onClick="markDelete()">|;
  317.     print "</th></tr>\n";
  318.     print "\n</table></center>\n";
  319.  
  320.     print &js_buttons('doit','Ok','onClick="markOther()"','onClick="markOther()"');
  321.     
  322.     print '</form>';
  323. }
  324.  
  325. sub getGateways {
  326.     undef @routes;
  327.  
  328.     open(GATE, "< $conf");
  329.     while (<GATE>) {
  330.     @items = split(/\s+/);
  331.     if ($items[0] ne 'route' && $items[1] ne 'add') { next; }
  332.     push(@routes,$items[3]);
  333.     if ($items[3] eq $_[0]) {
  334.         $type = substr($items[2],1);
  335.         $rout = $items[4];
  336.     }
  337.     }
  338.     close(GATE);
  339. }
  340.